package com.liu.controller;

import com.liu.exception.ValidateException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaseController {


    protected Map valid(BindingResult result ){
        Map map = new HashMap();
        //获取验证的错误信息
        List<FieldError> list = result.getFieldErrors();
        if (list.size() > 0) {
            //说明有错误信息 验证没有通通过
            //遍历错误集合
            list.forEach(fe -> {
                map.put(fe.getField(), fe.getDefaultMessage());
            });
            //有错误信息 抛出异常
            throw new ValidateException(map);
        }
        return map;
    }



}
